package com.example.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.video.util.RoleUtils;
import com.example.video.entity.Device;
import com.example.video.mapper.DeviceMapper;
import com.example.video.service.IDeviceService;
import jakarta.annotation.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qiusuyang
 * @since 2024-05-10
 */
@Service
public class DeviceServiceImpl implements IDeviceService {
    @Resource
    private DeviceMapper mapper;
    @Override
    public List<Device> getList(Integer page, Integer size) {
        QueryWrapper<Device> qwrapper = new QueryWrapper<>();
        IPage<Device> iPage = mapper.selectPage(new Page<>(page,size),qwrapper);
        return iPage.getRecords();
    }
    @Override
    public long  GetCount() {
        return mapper.selectCount(new QueryWrapper<>());
    }

    @Override
    public List<Device> getListByRole(Integer page, Integer size, String role) {
        QueryWrapper<Device> qwrapper = new QueryWrapper<>();
        qwrapper.eq("role",role);
        IPage<Device> iPage = mapper.selectPage(new Page<>(page,size),qwrapper);
        return iPage.getRecords();
    }

    @Override
    public long getCountByROle(String role) {
        return mapper.selectCount(new QueryWrapper<Device>().eq("role",role));
    }

    @Override
    public List<Device> getListByStatus(Integer page, Integer limit, Integer status) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 获取用户的所有权限
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        // 提取并格式化角色
        List<String> formattedRoles = RoleUtils.extractAndFormatRoles(authorities);

        QueryWrapper<Device> qwrapper = new QueryWrapper<>();
        qwrapper.eq("status", status);
        // 如果formattedRoles中包含[[ROLE_ADMIN]]，则返回表中所有数据
        if (formattedRoles.size() == 1 && formattedRoles.get(0).equals("[ROLE_ADMIN]")) {
        } else {
            // 设置查询条件，匹配formattedRoles列表中的任一角色
            if (!formattedRoles.isEmpty()) {
                qwrapper.in("role", formattedRoles);
            }
        }

        IPage<Device> ipage = mapper.selectPage(new Page<>(page,limit),qwrapper);
        return ipage.getRecords();
    }

    @Override
    public Device getItemByDeviceId(String deviceId) {
        return mapper.selectOne(new QueryWrapper<Device>().eq("device_id",deviceId));
    }

    @Override
    public void delete(String deviceId) {
        mapper.delete(new QueryWrapper<Device>().eq("device_id",deviceId));
    }


}
